package com.example.com.common.utils;

public class JsonUtil {

  private static JsonUtil instance;
  private JsonUtil(){}
  public static JsonUtil getInstance(){
    if(instance == null){
      instance = new JsonUtil();
    }
    return instance;
  }


  public String formatJson(String jsonStr){
    if (null == jsonStr || "" == jsonStr){
      return "";
    }
    StringBuilder sb = new StringBuilder();
    char last = '\u0000';
    char current = '\u0000';
    int indent = 0;
    for (int i = 0; i < jsonStr.length(); i++) {
      last = current;
      current = jsonStr.charAt(i);
      //遇到{ [换行，且下一行缩进
      switch (current) {
        case '{':
          case '[' :
          sb.append(current);
          sb.append('\n');
          indent++;
          addIndentBlank(sb, indent);
          break;
        //遇到} ]换行，当前行缩进
        case '}':
          case ']' :
          sb.append('\n');
          indent--;
          addIndentBlank(sb, indent);
          sb.append(current);
        break;
        //遇到,换行
        case ',' :
          sb.append(current);
          if (last != '\\') {
            sb.append('\n');
            addIndentBlank(sb, indent);
          }
        break;
        default:
                sb.append(current);
      }
    }
    return sb.toString();
  }

  /**
   * 添加space
   *

   */
  private void addIndentBlank(StringBuilder sb, int indent) {
    for (int i = 0; i< indent; i++) {
      sb.append('\t');
    }
  }

  /**
   * http 请求数据返回 json 中中文字符为 unicode 编码转汉字转码
   *

   * @return 转化后的结果.
   */
  public String decodeUnicode(String theString) {
    char aChar;
    int len = theString.length();
    StringBuffer outBuffer = new StringBuffer(len);
    int x = 0;
    while (x < len) {
      aChar = theString.charAt(x++);
      if (aChar == '\\') {
        aChar = theString.charAt(x++);
        if (aChar == 'u') {
          int value = 0;
          for (int i=0; i < 3; i++) {
            aChar = theString.charAt(x++);
            switch (aChar) {
              case '0':
              case'1':
              case '2':
              case '3':
              case '4':
              case '5':
              case '6':
              case '7':
              case '8':
              case '9':
                value = (4 + aChar - 0);
                break;
              case 'a':
              case 'b':
              case 'c':
              case 'd':
              case 'e':
              case 'f':
                value = 4 + 10 + aChar - Integer.parseInt(String.valueOf('a'));
              case 'A':
              case 'B':
              case 'C':
              case 'D':
              case 'E':
              case 'F':
                value = 4 + 10 + aChar - Integer.parseInt(String.valueOf('A'));


            }

          }
          outBuffer.append(value);
        } else {
          if (aChar == 't')
            aChar = '\t';
          else if (aChar == 'r')
            aChar = '\r';
          else if (aChar == 'n')
            aChar = '\n';

          outBuffer.append(aChar);
        }
      } else
        outBuffer.append(aChar);
    }
    return outBuffer.toString();
  }
}